package com.Guigu.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 魏正想
 * @version 1.0
 * @date 2025/9/23  20:16
 */

/**
 * 邮件发送
 */
@RestController
public class MailController {

    @Value("${spring.mail.username}")
    private String MAIL_SENDER;

    /**
     * 注入QQ发送邮件的bean
     */
    @Resource
    private JavaMailSender javaMailSender;

    @PostMapping("/sendMail")
    public void sendMail(String mail,String content) {
        MailBean mailBean = new MailBean();
        //接收者
        mailBean.setRecipient(mail);
        //标题
        mailBean.setSubject("喂喂喂");
        //内容主体
        mailBean.setContent(content);
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送者
            mailMessage.setFrom(MAIL_SENDER);
            //接收者
            mailMessage.setTo(mailBean.getRecipient());
            //邮件标题
            mailMessage.setSubject(mailBean.getSubject());
            //邮件内容
            mailMessage.setText(mailBean.getContent());
            //发送邮箱
            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            throw new RuntimeException("邮件发送失败");
        }
    }
}
